﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using NJ.Common.Extension;
using NJ.Dto.RoleMenu;
using NJ.Entity;
namespace NJ.Service.RoleMenu
{
    public class RoleMenuService : BaseService, IRoleMenuService
    {
        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        public async Task<List<GetMenuRolesDto>> GetMenusAsync()
        {
            var data = await GetAll<MenuEntity>().Select(t => new GetMenuRolesDto
            {
                Icon = t.Icon,
                Id = t.Id,
                Leader = t.Leader,
                PId = t.PId,
                Url = t.Url,
                Title = t.Title,
                Roles = GetAll<RoleMenuEntity>().Join(GetAll<RoleEntity>(), a => a.RoleId, b => b.Id, (a, b) => new { a, b }).Where(rm => rm.a.MenuId == t.Id).Select(r => new RoleDto
                {
                    Id = r.b.Id,
                    RoleName = r.b.RoleName
                }).ToList()
            }).ToListAsync();
            return data;
        }
        /// <summary>
        /// 获取角色的菜单树
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public async Task<List<GetHomeMenuTreeDto>> GetHomeMenuTreeAsync(int RoleId)
        {
            var roleMenuData = await GetAll<MenuEntity>().Join(
                GetAll<RoleMenuEntity>(),
                menu => menu.Id,
                roleMenu => roleMenu.MenuId,
                (menu, role) => new
                {
                    menu.Id,
                    menu.Icon,
                    menu.PId,
                    menu.Title,
                    menu.Url,
                    menu.Leader,
                    role.RoleId
                })
                .Where(rm => rm.RoleId == RoleId && !GetAll<MenuEntity>(t => t.PId == rm.Id).Any())//只获取最后一级菜单
                .ToListAsync();

            var list_child = new List<GetHomeMenuTreeDto>();

            var ids = new List<int>();
            foreach (var dto in roleMenuData)
            {
                var array = dto.Leader.Split('-');
                foreach (var item in array)
                {
                    if (item.To<int>() > 0)
                    {
                        ids.Add(item.To<int>());
                    }
                }
                list_child.Add(new GetHomeMenuTreeDto
                {
                    Exist = false,
                    Icon = dto.Icon,
                    MenuId = dto.Id,
                    PId = dto.PId,
                    Title = dto.Title,
                    Url = dto.Url
                });
            }

            var list_parent = await GetAll<MenuEntity>(t => ids.Contains(t.Id)).Select(t => new GetHomeMenuTreeDto
            {
                Exist = true,
                Icon = t.Icon,
                MenuId = t.Id,
                PId = t.PId,
                Title = t.Title,
                Url = t.Url
            }).ToListAsync();

            var list = list_child.Concat(list_parent).ToList();

            return list;

        }

        public async Task SaveAsync(CreateRoleMenuDto input)
        {
            DeleteBatch(GetAll<RoleMenuEntity>(r => r.MenuId == input.MenuId));

            var list = new List<RoleMenuEntity>();

            for (int i = 0; i < input.RoleIds.Length; i++)
            {
                list.Add(new RoleMenuEntity
                {
                    MenuId = input.MenuId,
                    RoleId = input.RoleIds[i]
                });
            }
            AddBatch(list);

            await SaveChangesAsync();
        }

        public async Task<List<GetRoleAndCheckMenuExistDto>> GetRoleAndCheckMenuExistAsync(int Id)
        {
            var list = await GetAll<RoleEntity>(t => t.Id != 100).Select(r => new GetRoleAndCheckMenuExistDto
            {
                Id = r.Id,
                RoleName = r.RoleName,
                Exist = GetAll<RoleMenuEntity>(rm => rm.RoleId == r.Id && rm.MenuId == Id).Any()
            }).ToListAsync();
            return list;
        }
    }
}